package com.gxc.service.impl;

import com.gxc.dao.DeptRepository;
import com.gxc.entity.Dept;
import com.gxc.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

/**
 * @author GongXincheng
 * @since 2019-08-19 21:39
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Resource
    private DeptRepository deptRepository;

    @Override
    @HystrixCommand(fallbackMethod = "processHystrix")
    public Dept find(Long id) {
        Dept dept = deptRepository.findById(id).orElse(null);
        if (dept == null) {
            throw new RuntimeException("id = "+ id  +"未查询到记录...");
        }
        return dept;
    }

    public Dept processHystrix(@PathVariable Long id) {
        return new Dept(id, "error" , "error_DB");
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public Boolean add(Dept dept) {
        deptRepository.save(dept);
        return true;
    }

    @Override
    public List<Dept> list() {
        return deptRepository.findAll();
    }
}
